<script setup>
import { ref, computed } from 'vue'
import { userEdit } from '@/api/user'
import { useUserStore } from '@/stores'
const {
  userInfo: { username, nickname, email, id },
  getInfo
} = useUserStore()
const formData = ref({
  username,
  nickname,
  email,
  id
})
const form = ref()
const rules = computed(() => ({
  nickname: [
    { required: true, message: '昵称不能为空', trigger: 'blur' },
    {
      min: 2,
      max: 10,
      message: '昵称长度必须在2-10个字符之间',
      trigger: 'blur'
    }
  ],
  email: [
    { required: true, message: '邮箱不能为空', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
}))

const submitForm = async () => {
  await form.value.validate()
  await userEdit(formData.value)
  getInfo()
  ElMessage.success('修改成功')
}
</script>
<template>
  <page-container title="基本资料">
    <el-form :model="formData" :rules="rules" ref="form">
      <el-form-item label="登录名称" prop="username">
        <el-input v-model="formData.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input v-model="formData.nickname"></el-input>
      </el-form-item>
      <el-form-item label="用户邮箱" prop="email">
        <el-input v-model="formData.email"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交修改</el-button>
      </el-form-item>
    </el-form>
  </page-container>
</template>
